#include<func.h>
#include"threadPool.h"
#include"head.h"
#include"taskQueue.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}

int OrderJudge(task_t * pTask, virDir_t *preVd){
    char buf[1024]={0};
    if(strncmp(pTask->buf,"cd ",3)==0){
        //printf("cd 前 : id = %s, pre_ id = %s\n", preVd->id, preVd->pre_id);
        cd(pTask, preVd);
        //printf("cd 后: id = %s, pre_ id = %s\n", preVd->id, preVd->pre_id);
    }
    else if(strncmp(pTask->buf,"cd",2)==0){
        cd(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"ls ",3)==0){
        ls(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"ls",2)==0){
        ls(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"puts ",5)==0){
       //cd(pTask);
       uploadFile(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"gets ",5)==0){
        //puts("gets");
        downloadFile(pTask);
    }
    else if(strncmp(pTask->buf,"remove ",7)==0){
        //puts("remove");
        removeFile(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"pwd ",4)==0){
        printf("id = %s, pre_ id = %s\n", preVd->id, preVd->pre_id);
        char returnPath[1024] = {0};
        myPwd(returnPath, preVd);
        send(pTask->netFd,returnPath,strlen(returnPath),0);
        //puts("pwd");
    }
    else if(strncmp(pTask->buf,"mkdir ",6)==0){
        myMkdir(pTask, preVd);
    }
    else if(strncmp(pTask->buf,"rmdir ",6)==0){
        //removeFile(pTask);
        puts("rmdir");
    }
    else{
        //send(pTask->netFd,"命令错误，重新输入！",30,0);
        send(pTask->netFd, "Invalid command, please re-enter!", 33, 0);
        return -1;
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        task_t *pTask;//新建一个task来接收队首的任务
        pthread_mutex_lock(&pThreadPool->mutex);
        while (pThreadPool->taskQueue.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        handleEvent(pTask);
    }
}
int handleEvent(task_t* pTask){
    task_t mypTask = *pTask; //接收下来
    virDir_t *preVd = (virDir_t*)calloc(1, sizeof(virDir_t));       //虚拟文件表信息
    puts("welcome into handEvent!");
    int netFd = mypTask.netFd;
    //usrConnectRecord(mypTask);//记录日志信息cat /var/log/syslog查看日志信息
    //从这开始是密码验证需要的
    while(1){
        //判断是注册还是登录
        char way[20] = {0};
        recv(netFd, way, sizeof(way), 0);
        puts(way);
        printf("way = %d\n", atoi(way));
        //1.注册    2.登录

        char userName[20] = {0};
        char password[20] = {0};
        int ret = recv(netFd, userName, sizeof(userName), 0);
        ERROR_CHECK(ret,-1,"recv1");
        //puts(userName);
        ret = recv(netFd, password, sizeof(password), 0);
        ERROR_CHECK(ret,-1,"recv2");
        //puts("1");

        //注册-->判定用户是否存在，存在就跳转登录;不存在就注册成功，存进数据库后跳转登录
        //先获取数据库用户密码表（userPasswd）中的用户，筛选判断
        
        userinfo_t *pInfo = (userinfo_t *)calloc(1, sizeof(userinfo_t));    //用户密码信息
        ret = getUserPasswd(pInfo,userName);
        if (atoi(way) == 1) {
            if (ret == 0) {
                //用户已存在
                send(netFd, "-2", 3, 0);
            }
            else{
                ret = chdir("/home/hcr/hcrprog_2/Filehome");
                ERROR_CHECK(ret, -1, "chdir");
                //注册成功，存储到数据库
                userAdd(userName, password);
                send(netFd, "1", 2, 0);
            }
        }
        else if (atoi(way) == 2){
            //要改动
            if (ret == 0) {
                //密码正确
                //切换当前目录至存储文件的目录
                ret = chdir("/home/hcr/hcrprog_2/Filehome");
                ERROR_CHECK(ret, -1, "chdir");
                char useCiphertext[1024] = {0};
                strcpy(useCiphertext, crypt(password, pInfo->salt));
                if (!strcmp(pInfo->ciphertext, useCiphertext)) {
                    send(netFd, "2", 2, 0);
                    //获取当前工作目录信息
                    //virDir_t *preVd = (virDir_t*)calloc(1, sizeof(virDir_t));
                    getVirDirInfo(preVd, userName);
                    printf("登录后: id = %s, preid = %s\n", preVd->id, preVd->pre_id);
                    break;
                }
                //密码错误
                else {
                    send(netFd, "-1", 3, 0);
                }
            }
            else {
                //用户不存在
                send(netFd, "-3", 3, 0);
            }
        }
        else {
            //无效选择
            send(netFd, "-4", 3, 0);
        }        
    }

    
    usrConnectRecord(mypTask);//记录登录时间
    while (1){
        bzero(mypTask.buf,sizeof(mypTask.buf));
        int ret = recv(mypTask.netFd,mypTask.buf,sizeof(mypTask.buf),0);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        usrCommandRecord(mypTask);
        OrderJudge(&mypTask, preVd);
    }
    close(netFd);
    return 0;
}